const { DataTypes } = require('sequelize')
const sequelize = require('../config/database')

const UserDepartment = sequelize.define('UserDepartment', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '关联ID'
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '用户ID'
  },
  department_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '部门ID'
  },
  position: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '职位'
  },
  is_primary: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
    comment: '是否主部门'
  }
}, {
  tableName: 'user_departments',
  timestamps: true,
  indexes: [
    {
      fields: ['user_id'],
      name: 'user_departments_user_id_index'
    },
    {
      fields: ['department_id'],
      name: 'user_departments_department_id_index'
    },
    {
      unique: true,
      fields: ['user_id', 'department_id'],
      name: 'user_departments_unique_index'
    }
  ]
})

module.exports = UserDepartment 